package com.qf.common.utils;

import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static String longToDateYMD(long i) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date(i);
        String str = sdf.format(date);
        return str;
    }

    /**
     * MaNongXF
     * 判断时间是否处于某个时间段内
     *
     * @param time 需要比较的时间
     * @param from 起始时间
     * @param to   结束时间
     * @return
     */
    public static boolean belongCalendar(Date time, Date from, Date to) {
        Calendar date = Calendar.getInstance();
        date.setTime(time);
        Calendar after = Calendar.getInstance();
        after.setTime(from);
        Calendar before = Calendar.getInstance();
        before.setTime(to);
        if (date.after(after) && date.before(before)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isInsert() {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        Date now = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            now = df.parse(df.format(new Date()));
            beginTime = df.parse("00:00");
            endTime = df.parse("05:00");
        } catch (Exception e) {
            e.printStackTrace();
        }

        Boolean flag = belongCalendar(now, beginTime, endTime);
        return !flag;
    }

    public static LocalDateTime parseLong(Long l) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(l), ZoneId.systemDefault());
        return localDateTime;
    }

    public static void main(String[] args) {
        LocalDateTime localDateTime = parseLong(1810441374666L);
        System.out.println(localDateTime);

    }

    public static String longToDate_ymd(Long i) {
        String str = "";
        if (i != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date(i);
            str = sdf.format(date);
        }
        return str;
    }

    public static String longToDate_ymdhms(long i) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(i);
        String str = sdf.format(date);
        return str;
    }

    public static long stringToDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = (Date) sdf.parse(str);
            return date.getTime();
        } catch (ParseException e) {
            return 0;
        }
    }

    public static long stringToDateH(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH");
        Date date = null;
        try {
            date = (Date) sdf.parse(str);
            return date.getTime();
        } catch (ParseException e) {
            return 0;
        }
    }


    public static boolean equalDate(Date date1, Date date2, String type) {
        boolean flag = true;
        int date1Year = DateUtil.year(date1);
        int date1Month = DateUtil.month(date1);
        int date1Day = DateUtil.dayOfMonth(date1);

        int date2Year = DateUtil.year(date2);
        int date2Month = DateUtil.month(date2);
        int date2Day = DateUtil.dayOfMonth(date2);

        if ("day".equals(type)) {
            flag = date1Year == date2Year && date1Month == date2Month && date1Day == date2Day;
        } else if ("month".equals(type)) {
            flag = date1Year == date2Year && date1Month == date2Month;
        } else if ("year".equals(type)) {
            flag = date1Year == date2Year;
        } else {
            flag = false;
        }
        return flag;
    }
}
